package com.beehoo.dcportal.action;

import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.CommentBean;
import com.beehoo.dcportal.service.ICommentServ;
import com.beehoo.dcportal.utils.CommonUtils;
import com.opensymphony.xwork2.ModelDriven;

public class CommentAction extends BaseAction implements ModelDriven<CommentBean> {
	
	private static final long serialVersionUID = -2199461558945500308L;
	private ICommentServ commentServ; //评论服务对象
	
	public ICommentServ getCommentServ() {
		return commentServ;
	}

	public void setCommentServ(ICommentServ commentServ) {
		this.commentServ = commentServ;
	}
	
	private CommentBean comment = new CommentBean();
	@Override
	public CommentBean getModel() {
		return comment;
	}

	public String add(){
		String sVcode = CommonUtils.nvlToString(getSession().get(Constant.SESSION_VALIDATECODE));
		getSession().remove(Constant.SESSION_VALIDATECODE);
		
		if(null == comment.getVCode() 
				|| "".equals(comment.getVCode()) 
				|| !sVcode.equalsIgnoreCase(comment.getVCode())){
			setMessage("验证码错误");
			return "add";
		}
		if(null == comment.getCommentAuthor() || "".equals(comment.getCommentAuthor())){
			comment.setCommentAuthor("匿名人士");
		}
		comment.setStreetId(paramMap.get("streetId"));
		comment.setAuthorAddr(CommonUtils.getIpAddr(getRequest()));
		comment.setPublishTime(CommonUtils.getDateTime());
		
		try {
			//对身份证号码验证
			getCommServ().validateIDCardNum(comment.getAuthorIdcard());
			commentServ.addComment(comment);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return "add";
		}
		setMessage("评论成功，等待审核");
		return "add";
	}
}
